package 分类.二叉树;


import java.util.LinkedList;
import java.util.Queue;

class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
public class 二叉树的最大深度 {
    public int maxDepth(TreeNode root) {
        if (root == null) return 0;
        int leftDepth = maxDepth(root.left);
        int rightDepth = maxDepth(root.right);
        return 1 + Math.max(leftDepth, rightDepth);

    }

    public int maxDepth_02(TreeNode root) {
        if(root==null){
            return 0;
        }
        int ans=0;
        Queue<TreeNode> queue=new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()){
            int sz= queue.size();
            for (int i=0;i<sz;i++){
                TreeNode r=queue.poll();
                if(r.right!=null){
                    queue.add(r.right);
                }
                if(r.left!=null){
                    queue.add(r.left);
                }
            }
            ans++;
        }
        return ans;
    }
}